package com.dms.service.impl;

import com.dms.mapper.DormitoryManageMapper;
import com.dms.pojo.Dormitory;
import com.dms.pojo.Student;
import com.dms.service.DormitoryManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className: DormitoryManageServiceImpl
 * @package: com.dms.service.impl
 * @description  宿舍管理模块业务逻辑实现类
 * @author: sk
 */
@Service
public class DormitoryManageServiceImpl implements DormitoryManageService {

    @Autowired
    DormitoryManageMapper dormitoryManageMapper;

    /**
     *  判断该楼栋该宿舍号是否已存在
     * @param buildingId
     * @param dormitoryRoom
     * @return
     */
    @Override
    public boolean checkRoomByBuildingNumber(int buildingId, String dormitoryRoom) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("buildingId", buildingId);
        map.put("dormitoryRoom", dormitoryRoom);
        Dormitory dormitory = dormitoryManageMapper.checkRoomByBuildingNumber(map);
        System.out.println(dormitory);
        return dormitory == null ? true : false;
    }

    /**
     * 添加寝室
     * @param dormitory
     * @return
     */
    @Override
    public int addDormitory(Dormitory dormitory){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("buildingId", dormitory.getBuildingId());
        map.put("dormitoryRoom", dormitory.getDormitoryRoom());
        map.put("dormitorySpendable", dormitory.getDormitorySpendable());
        return dormitoryManageMapper.addDormitory(map);
    }

    /**
     * 模糊分页查询 获取楼栋寝室列表
     * @param query
     * @param select
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public List<Dormitory> getDormitoryListLimit(String query, int select, int pageNum, int pageSize){
        Map<String, Object> map = new HashMap<String, Object>();

        pageNum = (pageNum - 1) * pageSize;
        map.put("query", (query.equals("") || query == null) ? null : query);
        map.put("select", select==0 ? null : select);
        map.put("start", pageNum );
        map.put("size", pageSize);

        //分页模糊查询宿舍列表
        List<Dormitory> dormitoryList = dormitoryManageMapper.getDormitoryListLimit(map);
        //遍历每个宿舍，将每个宿舍的人员重新全部获取
        for (Dormitory dormitory : dormitoryList) {
            int buildingId = dormitory.getBuilding().getBuildingId();
            String room = dormitory.getDormitoryRoom();
            List<Student> studentList = getDormitoryStudentList(buildingId,room);
            dormitory.setStudentList(studentList);
        }

        return dormitoryList;
    }

    /**
     * 获取寝室所住学生列表信息
     * @param buildingId
     * @param dormitoryRoom
     * @return
     */
    @Override
    public List<Student> getDormitoryStudentList(int buildingId, String dormitoryRoom){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("buildingId", buildingId);
        map.put("dormitoryRoom", dormitoryRoom);
        return dormitoryManageMapper.getDormitoryStudentList(map);
    }

    /**
     * 获取 楼栋寝室total
     * @param query
     * @param select
     * @return
     */
    @Override
    public int getDormitoryTotal(String query, int select){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("query", query.equals("") ? null : query);
        map.put("select", select == 0 ? null : select);
        return dormitoryManageMapper.getDormitoryTotal(map);
    }

    /**
     * 获取寝室信息
     * @param buildingId
     * @param dormitoryRoom
     * @return
     */
    @Override
    public Dormitory getDormitoryInfo(int buildingId, String dormitoryRoom){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("buildingId", buildingId);
        map.put("dormitoryRoom", dormitoryRoom);
        return dormitoryManageMapper.getDormitoryInfo(map);
    }

    /**
     * 修改寝室信息
     * @param dormitory
     * @return
     */
    @Override
    public int editDormitory(Dormitory dormitory){
        return dormitoryManageMapper.editDormitory(dormitory);
    }

    /**
     * 1、更新原寝室人员的dormitoryId为null
     * 2、删除寝室信息
     * @param dormitoryId     @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int batchUpdateStudentDormitoryId(int dormitoryId){
        int num = dormitoryManageMapper.batchUpdateStudentDormitoryId(dormitoryId);
        if (num > 0)
            num += deleteDormitory(dormitoryId);
        return num;
    }

    /**
     * 2、并删除寝室信息
     * @param dormitoryId
     * @return
     */
    @Override
    public int deleteDormitory(int dormitoryId){
        return dormitoryManageMapper.deleteDormitory(dormitoryId);
    }


}
